package com.ht.medicaltreatment.viewmodel

import androidx.lifecycle.viewModelScope
import com.blankj.utilcode.util.ToastUtils
import com.ht.medicaltreatment.api.RetrofitUtils
import com.ht.medicaltreatment.bean.CentrifugeAddBean
import com.ht.medicaltreatment.bean.CentrifugeSampleListBean
import com.ht.medicaltreatment.bean.ProjectBean
import com.xgst.driverlesscars.api.ApiService
import com.xgst.lib.http.ApiResult
import kotlinx.coroutines.launch


class CentrifugeAddSampleViewModel:BaseViewModel() {
    //根据名称模糊查询样本
    fun findSampleByName(id:String,sampleName:String,onSuccess:(MutableList<CentrifugeSampleListBean>)->Unit
                         , onError:(String)->Unit){
        viewModelScope.launch {
            val bean = mApi.findSampleByName(id,sampleName)
            when(bean){
                is ApiResult.Success -> onSuccess(bean.data.data!!)
                is ApiResult.Error -> onError(bean.errorMessage)
            }
        }
    }

    //项目
    fun projects(onResult:(MutableList<ProjectBean>)->Unit){
        viewModelScope.launch {
            val bean = RetrofitUtils.initRetrofit(ApiService::class.java).projects()
            when(bean){
                is ApiResult.Success -> onResult(bean.data.data!!)
                is ApiResult.Error ->  ToastUtils.showShort(bean.errorMessage)
            }
        }
    }

    //把样本添加到离心样本表中
    fun addCentrifuge(sampleArrId: String, centrifugeId:String, onSuccess:(CentrifugeAddBean?)->Unit, onError:(String)->Unit){
        viewModelScope.launch {
            val bean = mApi.addCentrifuge(sampleArrId,centrifugeId)
            when(bean){
                is ApiResult.Success -> onSuccess(bean.data.data)
                is ApiResult.Error -> onError(bean.errorMessage)
            }
        }
    }
}